export class Utils {
    public convertToTransparentColor(color: string, opacity: number) {
        // 提取RGB值并计算新的RGBA值
        var rgb = color.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
        if (rgb !== null && typeof rgb[1] === 'string' && typeof rgb[2] === 'string' && typeof rgb[3] === 'string') {
            return `rgba(${parseInt(rgb[1])}, ${parseInt(rgb[2])}, ${parseInt(rgb[3])}, ${opacity})`;
        } else {
            const colorValue = color.replace(/^#/,"");
            const colorData = {
                r: 0,
                g: 0,
                b: 0,
                a: opacity
            };
            let finalColor = color;
            if(colorValue.length === 3) {
                colorData.r = parseInt(colorValue[0] + colorValue[0], 16);
                colorData.g = parseInt(colorValue[1] + colorValue[1], 16);
                colorData.b = parseInt(colorValue[2] + colorValue[2], 16);
                finalColor = `rgba(${colorData.r}, ${colorData.g},${colorData.b},${opacity})`;
            } else if(colorValue.length === 6) {
                colorData.r = parseInt(colorValue.substring(0,2), 16);
                colorData.g = parseInt(colorValue.substring(2,4), 16);
                colorData.b = parseInt(colorValue.substring(4,6), 16);
                finalColor = `rgba(${colorData.r}, ${colorData.g},${colorData.b},${opacity})`;
            }
            return finalColor;
        }
    }
    public static getInstance(): Utils {
        if(!(Utils as any).instance) {
            (Utils as any).instance = new Utils();
        }
        return (Utils as any).instance;
    }
}
